package com.teragadgets.android.comic;

import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

import com.teragadgets.android.comic.datamodel.WebComic;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class WebComicListActivity extends Activity implements Observer,
		OnItemClickListener {

	private ListView webComicList;
	private WebComicListModel model;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// Initialize Model
		model = new WebComicListModel(this);
		model.addObserver(this);

		webComicList = (ListView) this.findViewById(R.id.comicList);
		webComicList.setAdapter(new ComicAdapter(this, model.getWebComics(),
				R.layout.webcomicrow, R.id.webComicName));
		webComicList.setOnItemClickListener(this);
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		model.updateWebComics(this);
	}

	@Override
	public void update(Observable observable, Object data) {
		((ComicAdapter) webComicList.getAdapter()).notifyDataSetChanged();
		webComicList.refreshDrawableState();
	}

	private WebComicListActivity getThis() {
		return this;
	}

	private class ComicAdapter extends ArrayAdapter<WebComic> {

		public ComicAdapter(Context context, Vector<WebComic> webComics,
				int layout, int textResource) {
			super(context, layout, textResource, webComics);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = super.getView(position, convertView, parent);

			ImageView logo = (ImageView) view.findViewById(R.id.webComicLogo);
			Bitmap icon = getItem(position).getIcon();
			if (icon == null)
				getItem(position).addObserver(getThis());
			logo.setImageBitmap(icon);
			return view;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		final WebComic selectedComic = (WebComic) parent.getAdapter().getItem(
				position);

		final Context ctx = this;

		final Intent[] i = new Intent[1];
		i[0] = new Intent(this, ComicListActivity.class);
		i[0].putExtra("webComicID", selectedComic.getID());

		if (selectedComic.getOrder() == WebComic.NOT_SET) {
			new AlertDialog.Builder(this).setItems(
					new String[] { "Start from Beginning", "Start from End" },
					new OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							int order = which == 0 ? WebComic.ASC
									: WebComic.DESC;
							model.save(ctx, selectedComic, order);
							startActivity(i[0]);
						}
					}).show();
		} else {
			startActivity(i[0]);
		}
	}

	private final int MENU_MANAGE = 0;

	/* Creates the menu items */
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, MENU_MANAGE, 0, "Manage").setIcon(
				android.R.drawable.ic_menu_add);
		return true;
	}

	/* Handles item selections */
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU_MANAGE:
			Intent i = new Intent(this, WebComicManagerActivity.class);
			startActivity(i);
			return true;
		}
		return false;
	}
}